package iamutkarshtiwari.github.io.ananas.editimage.utils;


import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Optional;

public class DensityUtil {

    public static int dip2px(Context context, float dpValue) {
//        final float scale = context.getResources().getDisplayMetrics().density;
        final double scale = getDensityDpi(context);
        return (int) ((double) dpValue * scale + 0.5D);
    }

    public static int px2dip(Context context, float pxValue) {
//        final float scale = context.getResources().getDisplayMetrics().density;
        final double scale = getDensityDpi(context);
        return (int) ((double) pxValue / scale + 0.5D);
    }

    public static float getDensityDpi(Context context) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = display.get().getAttributes();
        return displayAttributes.densityDpi;
    }
}
